package com.authority.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.authority.common.AdminConstants;
import com.authority.entry.AuthOrganDO;
import com.authority.service.AuthOrganService;
import com.authority.service.UserService;
import com.google.gson.Gson;
import com.suixun.util.Constant;

/** 
 * @Description: 组织管理
 * @author chenlushun
 * @date 2015年10月20日 下午2:19:09 
 *  
 */  
@Controller
@RequestMapping(value = "/organManager")
public class OrganManager  extends BaseManager{
	
	@Resource
	private AuthOrganService authOrganService;
	@Resource
	private UserService userService;
	
	/**
	 * 组织信息查询
	 * @return
	 */	
	@RequestMapping(value = "/queryOrgan.json")
	@ResponseBody
	public Object doQueryOrgan(){
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		List<AuthOrganDO> result =new ArrayList<AuthOrganDO>();
		try {
			Map<String,Object> map = new HashMap<>();
			if (!isAdmin()) {
				map.put("isNotAdmin", true);
				map.put("userid", getUserId());
			}
			result=authOrganService.queryAuthOrganDOList(map);
		} catch (Exception e) {
			logger("OrganManager doQueryOrgan:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
		return result;
	}
	
	/**
	 * 组织信息更新或新增
	 * @return
	 */	
	@RequestMapping(value = "/updateOrOrgan.json")
	@ResponseBody
	public Object doUpdateOrOrgan(String param){
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if (param == null) {
			param = "";
		}
		try{
			AuthOrganDO organ = new Gson().fromJson(param, AuthOrganDO.class);
			organ.setOperatorId(getUserId());
			if(organ.getId()!=null){
				int isUpdate = authOrganService.updateAuthOrgan(organ);
				if (isUpdate == -1) {
					return fail("组织id不存在");
				}else if (isUpdate == -2) {
					return fail("组织前缀已存在");
				}
			}else{
				authOrganService.insertAuthOrgan(organ);
			}
		}catch(Exception e){
			logger("OrganManager doUpdateOrOrgan:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
		return success(AdminConstants.CODE_STATUS_SUCCESS);
	}
	
	@RequestMapping(value = "/deleteAuthOrganByIds.json")
	@ResponseBody
	public Object deleteAuthOrganByIds(String param){
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if (param == null) {
			param = "";
		}
		try{
			AuthOrganDO organ = new Gson().fromJson(param, AuthOrganDO.class);
			if(organ.getId()==null) {
				return fail("id为空");
			}
			int isDelete = authOrganService.deleteAuthOrganByIds(organ.getId().toString());
			if (isDelete == -1) {
				return fail("该组织下存在账户，不能删除");
			}
		}catch(Exception e){
			logger("OrganManager deleteAuthOrganByIds:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
		return success(AdminConstants.CODE_STATUS_SUCCESS);
	}
	
}
